<template>
    <view mt-8 px-8>
        
        <w-tasklist-girl :tasklist="tasklist" v-if="tasklist" mt-4></w-tasklist-girl>
        <view v-else>
            <up-empty text="您还没有任务，快来创建第一个任务吧"></up-empty>
        </view>
        
    </view>
    <view bottom-50 fixed flex flex-col gap-3 w-full v-if="!shareTask">
        <up-button type="primary" open-type="share" text="发给某人" icon="share" @click="backDelay"></up-button>
        <up-button text="自己留着" @click="back"></up-button>
    </view>
    <view v-else view bottom-50 fixed flex flex-col w-full>
        <up-button text="我也发一个" @click="back"></up-button>
    </view>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { onLoad } from '@dcloudio/uni-app';
import { apiGetTaskByShare } from '@/api/task'
import { TaskList } from '@/types/task';
import { useTaskListStore } from '@/stores/tasklist';

    const shareTask = ref(false)
    const store = useTaskListStore()
    const tasklist = ref<TaskList|undefined>()
    onLoad((options:any)=>{
        // #ifdef MP-WEIXIN
        wx.reportEvent("share", {})
        // #endif
        console.log(options)
        
        if(options.sharecode){
            apiGetTaskByShare({
                share_code: options.sharecode
            }).then(res=>{
                tasklist.value = res                
            })
            shareTask.value = true
            return
        }
        const tmpList = store.taskList
        if(!tmpList){
            uni.showToast({
                title: '任务清单不存在',
                icon: 'none'
            })
            return  
        }
        tasklist.value = tmpList
    })
    const back = ()=>{
        //获取历史页面内数量
        const pages = getCurrentPages()
        const count = pages.length
        if(count<=1){
            uni.switchTab({
                url:'/pages/index/index'
            })
            return
        }
        uni.navigateBack()
    }

    const backDelay = ()=>{
        setTimeout(back,2000)
    }

</script>

<style lang="scss" scoped></style>